import config from 'config'
import { messages } from "share/common";
import httpFetch from 'share/httpFetch'
import errorMessage from 'share/errorMessage'
import { Modal} from "antd";

export default {

  //获取表单信息
  //isAll为true表示所有岗位 不传和false表示默认岗位
  getMyDocumentType(formType, userOID, isAll, applicationFormType) {
    return httpFetch.get(`${config.baseUrl}/api/custom/forms/my/available`, {
      formType: formType,
      userOID: userOID,
      isAll,
      applicationFormType
    });
  },

  //获取表单配置
  getCustomForm(formOID){
    return httpFetch.get(`${config.baseUrl}/api/custom/forms/${formOID}`)
  },

  //获取表单默认值
  getFormValue(params) {
    return httpFetch.get(`${config.baseUrl}/api/custom/form/user/default/values`, params)
  },

  //获取申请人消息
  getPrincipalsInfo(userId){
    return httpFetch.get(`${config.baseUrl}/api/company/configurations/user?userOID=${userId}`);
  },

  //加签人的选择范围人员
  postAddSignEnableScope(params){
    return httpFetch.post(`${config.baseUrl}/api/countersign/addSign/enable/and/scope`, params)
  },

  //保存
  handleSave(params){
    return httpFetch.post(`${config.baseUrl}/api/loanBill/draft`,params);
  },

  //得到详情信息
  getDetailInfo(params){
    return httpFetch.get(`${config.baseUrl}/api/loanBill/${params}`);
  },

  //提交
  handleSubmit(params){
    return new Promise( (resolve, reject)=> {
      httpFetch.post(config.baseUrl + '/api/loanBill/submit/v2', params)
        .then( (res) =>{
          resolve(res)
        })
        .catch( (err) =>{
          if(this.submitError(err)){
            return;
          }
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  submitError (e)  {
    let data = e.response.data;
    if (data && data.errorCode === '6017007'){
      Modal.warning({
        title: messages('loan-billing.key96')/*您的免费试用已经到期，请联系汇联易*/,
        content: '400 829 7878',
      });
      return true;
    }
  },
  //借款单对应借款行查询
  getLoanTable(params,isFinance = false){
    return httpFetch.get(`${config.baseUrl}/api/loan/line/query/by/loan/bill/id?loanBillId=${params}&isFinance=${isFinance}`)
  },

  //获取我的借款单分页数据
  getMyLoanTable(params,page,size){
    let tempParams = JSON.parse(JSON.stringify(params));
    Object.keys(tempParams).map(item => {
      if(item === 'status'){
        let temp = tempParams[item].match(/\d{4}/g);
        tempParams[item] = [];
        temp.map(value => {
          tempParams[item].push(value);
        });
      }
    });
    let rejectTypes = [];
    if(tempParams.status.length === 2){
      rejectTypes.push(tempParams.status[1]);
      tempParams.status.splice(1,1);
      tempParams.rejectTypes = rejectTypes;
    }
    return httpFetch.post(`${config.baseUrl}/api/loanBill/my?page=${page}&size=${size}`,tempParams);
  },

  //获取还款用户的账户信息
  getAccountBankInfo(applicationOID,userOID) {
    return httpFetch.get(`${config.baseUrl}/api/repayment/contact/bank?loanBillOID=${applicationOID}&userOID=${userOID}`);
  },

  //获取法人实体
  getLegalList(params) {
    return httpFetch.get(`${config.baseUrl}/api/v2/my/company/receipted/invoices?isAll=true`, params);
  },

  //获取公司
  getCompany(params){
    return httpFetch.get(`${config.baseUrl}/api/company/by/term?page=${params.page}&size=${params.size}&enabled=${true}&setOfBooksId=${params.setOfBooksId}`)
  },

  //根据公司id查询公司下的银行账户
  getCompanyBank(params){
    return httpFetch.get(`${config.baseUrl}/api/CompanyBank/selectByCompanyOID?page=${params.page}&size=${params.size}&companyOID=${params.companyOID}&isEnabled=${true}`)
  },

  //打印借款申请单
  printLoanApplication(loanBillOID) {
    return new Promise((resolve, reject) => {
      httpFetch.get(`${config.baseUrl}/api/loanBill/generate/pdf/${loanBillOID}`).then(res => {
        resolve(res)
      }).catch(err => {
        errorMessage(err.response);
        reject(err)
      })
    })
  },

  confirmPayment(params){
    return httpFetch.post(`${config.baseUrl}/api/reimbursement/batch/paid/confirm`, params)
  },

  recallRequest(params) {
    return httpFetch.post(`${config.baseUrl}/api/approvals/withdraw`, params)
  },

  deleteRequest(applicationOID) {
    return httpFetch.delete(`${config.baseUrl}/api/loanBill/${applicationOID}`)
  },

  //转账还款
  cardRepayment(params) {
    return httpFetch.post(`${config.baseUrl}/api/repayment/submit`, params)
  },

  //现金还款
  cashRepayment(params) {
    return httpFetch.post(`${config.baseUrl}/api/repayment/cash/submit`, params)
  },

  saveAndPrintRepayment(params) {
    return httpFetch.post(`/api/repayment/submitWithPrint`, params);
  },

  //新增借款行
  addLoanLine(params){
    return httpFetch.post(`${config.baseUrl}/api/loan/line`,params);
  },

  //获取行详情
  getLineDetail(id,isFinance){
    let url = `${config.baseUrl}/api/loan/line?id=${id}`;
    isFinance && (url += `&isFinance=${isFinance}`);
    return httpFetch.get(url);
  },

  //删除借款行
  deleteLoanLine(id){
    return httpFetch.delete(`${config.baseUrl}/api/loan/line?id=${id}`);
  },

  //审批通过
  handleRequestApprovePass(params) {
    return httpFetch.post(`${config.baseUrl}/api/approvals/pass`, params)
  },

  //审批驳回
  handleRequestApproveReject(params) {
    return httpFetch.post(`${config.baseUrl}/api/approvals/reject`, params)
  },

  //获取行详情
  getLineInfo(id){
    return httpFetch.get(`${config.baseUrl}/api/loan/line/query/extends/message/by/id?id=${id}`)
  },

  //获取行列表
  getTableLine(params,userOid,size){
    return httpFetch.get(`${config.baseUrl}/api/loan/line/query/pending/repayment/by/userOid?userOid=${userOid}&size=${size}`,params);
  },

  //获取配置里的表单类型
  getFormTypeList(formOid){
    return httpFetch.get(`${config.baseUrl}/api/query/custom/form/loan/types/by/formOid?formOid=${formOid}`)
  },

  //获取员工的银行账户
  getUserCompanyAccount(userOID){
    return httpFetch.get(`${config.baseUrl}/api/contact/bank/account/enable?userOID=${userOID}`)
  },

  //获取供应商银行账户
  getVendorAccount(companyOid,companyId){
    return httpFetch.get(`${config.baseUrl}/supplier/api/ven/info/by/company?companyOid=${companyOid}&companyId=${companyId}`)
  },

  //获取申请单详情
  getRequestDetail(OID) {
    return httpFetch.get(`${config.baseUrl}/api/application/${OID}`)
  },

  //获取流水详情
  getTransactionList(id) {
    return httpFetch.get(`${config.baseUrl}/api/loan/line/get/writeOff?loanLineId=${id}`)
  },

  getLoanEnum(){
    return httpFetch.get(`${config.baseUrl}/api/custom/enumerations/template/by/type?type=2105`)
  },

  //获取欠款金额
  getLoanInfo(params){
    return httpFetch.get(`${config.baseUrl}/api/loan/line/get/summary`,params);
  },

  getLoanMax(params){
    return httpFetch.get(`${config.baseUrl}/api/loan/line/query/loan/type/config/msg`,params)
  },

  getLoanHistory(params){
    return httpFetch.get(`${config.baseUrl}/api/repayment/query/by/loan/line/id?loanLineId=${params}`);
  },

  printRepayment(Oid){
    return httpFetch.get(`/api/repayment/generate/pdf/${Oid}`)
  },

  getJobInfo(jobId){
    return httpFetch.get(`/api/user/jobs/${jobId}`);
  },

  handleDelayDateOrToForeverAmount(params) {
    let url = `${config.baseUrl}/api/loan/line/delayDateOrToForeverAmount?loanLineId=${params.loanLineId}`
    if (params.delayDate) {
      url = url +  `&delayDate=${params.delayDate}`
    }
    url = url + `&delayFlag=${params.delayFlag}`
    if (params.toForeverAmount) {
      url = url + `&toForeverAmount=${params.toForeverAmount}`
    }
    url = url + `&remark=${params.remark}`
    return httpFetch.post(url);
  },

  handleUpdateDelayDateOrForeverAmount(params) {
    let url = `${config.baseUrl}/api/loan/line/updateDelayOrToforeverLoanLine?loanLineId=${params.loanLineId}`
    if (params.delayDate) {
      url = url +  `&delayDate=${params.delayDate}`
    }
    if (params.toForeverAmount) {
      url = url + `&toForeverAmount=${params.toForeverAmount}`
    }
    url = url + `&remark=${params.remark}`
    return httpFetch.post(url)
  },
  
  getIngDelayToforeverLoanBill(id) {
    return httpFetch.get(`${config.baseUrl}/api/loanBill/getIngDelayToforeverLoanBill?loanLineId=${id}`)
  },

  // 判断借款单能否新建借款行
  handleIsAddLoanLine(id) {
    return httpFetch.get(`${config.baseUrl}/api/loanBill/getIsAddLoanLine?loanBillId=${id}`)
  }

}
